using Mathmall.Administration.Data;
using Mathmall.Administration.Models;
using Microsoft.EntityFrameworkCore;

namespace Mathmall.Administration.Mapper;

public class CategoryBrandRelationMapper : BaseMapper
{
    public CategoryBrandRelationMapper(AdministrationContext context) : base(context)
    {
    }

    /// <summary>
    /// 新增品牌和分类关系
    /// </summary>
    /// <param name="categoryBrandRelation"></param>
    /// <returns></returns>
    public async Task<int> CreateCategoryBrandRelation(CategoryBrandRelation categoryBrandRelation)
    {
        await _context.CategoryBrandRelations.AddAsync(categoryBrandRelation);
        return await _context.SaveChangesAsync();
    }

    /// <summary>
    /// 根据品牌id查询关联的全部分类
    /// </summary>
    /// <param name="brandID"></param>
    /// <returns></returns>
    public async Task<List<CategoryBrandRelation>> GetCategoryBrandRelations(int brandID)
    {
        return await _context.CategoryBrandRelations
            .Where(categoryBrandRelation => categoryBrandRelation.BrandID == brandID).ToListAsync();
    }

    /// <summary>
    /// 删除品牌与分类的关联
    /// </summary>
    /// <param name="categoryBrandRelationID"></param>
    /// <returns></returns>
    public async Task<int> DeleteCategoryBrandRelation(int categoryBrandRelationID)
    {
        return await _context.CategoryBrandRelations
            .Where(categoryBrandRelation => categoryBrandRelation.CategoryBrandRelationID == categoryBrandRelationID)
            .ExecuteDeleteAsync();
    }

    /// <summary>
    /// 更新品牌名称
    /// </summary>
    /// <param name="brandID"></param>
    /// <param name="brandName"></param>
    /// <returns></returns>
    public async Task<int> UpdateBrandName(int brandID, string brandName)
    {
        return await _context.CategoryBrandRelations
            .Where(categoryBrandRelation => categoryBrandRelation.BrandID == brandID)
            .ExecuteUpdateAsync(setter => setter.SetProperty(c => c.BrandName, brandName));
    }

    /// <summary>
    /// 更新分类名称
    /// </summary>
    /// <param name="categoryID"></param>
    /// <param name="categoryName"></param>
    /// <returns></returns>
    public async Task<int> UpdateCategoryName(int categoryID, string categoryName)
    {
        return await _context.CategoryBrandRelations
            .Where(categoryBrandRelation => categoryBrandRelation.CategoryID == categoryID)
            .ExecuteUpdateAsync(setter => setter.SetProperty(c => c.CategoryName, categoryName));
    }
}